package com.atguigu.gmall.common.config.minio.config;

import com.atguigu.gmall.common.config.minio.propertie.MinioProperties;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//将minio客户端自动注入到spring容器中
@Configuration
public class MinioConfiguration {

//    @Autowired
//    MinioProperties minioProperties;
    @Bean//给客户端放一个组件，默认所有参数都是从容器中获取
    public MinioClient client(MinioProperties minioProperties) throws Exception {
        //1创建minio客户端
        MinioClient client = new MinioClient(
                minioProperties.getEndpoint(),//http://192.168.200.100:9000 是API连接地址  http://192.168.200.100:9001是访问管理地址
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
        //2查看桶是否存在
        if (!client.bucketExists(minioProperties.getBucketName())) {
            client.makeBucket(minioProperties.getBucketName());
        }
        return client;
    }
}
